<?php

declare(strict_types=1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use \think\facade\Db;
use think\facade\Cache;

class genbudget extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('app\command\genbudget')
            ->setDescription('the app\command\genbudget command');
    }

    protected function execute(Input $input, Output $output)
    {
        $GLOBALS['isin'] = $this->isin();
        $GLOBALS['sql'] = "insert into admin_budget_report (uniq_code,institutions_code,institutions_name,departments_code,district_name,status,office_data,subordinate_unit_data,staffing_data) values ";
        $GLOBALS['val'] = '{"onJob":{"count":0,"changeType":1,"changeNum":0},"retirement":{"count":0,"changeType":1,"changeNum":0},"retire":{"count":0,"changeType":1,"changeNum":0}}';
        Db::table('admin_budget')->whereBetween('id', '1, 15000')->chunk(1000, function ($rows) {
            foreach ($rows as $row) {

                $uniq_code = $row['district_code'] . $row['institutions_code'];

                // $key = 'gen_' . $uniq_code;
                // $is = Cache::get($key);
                if (!in_array($uniq_code, $GLOBALS['isin'])) {
                    $GLOBALS['sql'] .= "(";
                    echo $uniq_code . "\n";
                    $GLOBALS['sql'] .= "'" . $uniq_code . "','" . $row['institutions_code'] . "','" . $row['institutions_name'] . "','" . $row['departments_code'] . "','" . $row['district_name'] . "',";
                    // Cache::store('redis')->set($key, '1', 7200);
                    $GLOBALS['sql'] .= "'1','[\"\"]','[\"\"]','{$GLOBALS['val']}'),";
                }
            }
        });
        $s2 = substr($GLOBALS['sql'], 0, -1);
        // $s2 = ;
        echo $s2 . "\n";
        Db::query($s2);

        exit;
        // 指令输出
        // $output->writeln('app\command\genbudget');
    }

    public function isin()
    {
        $lists = Db::query("select uniq_code from admin_project_report");
        $arr = [];
        foreach ($lists as $row) {
            array_push($arr, $row['uniq_code']);
        }
        return $arr;
    }
}
